package com.douqu.game.core.util;

import com.bean.core.util.TimeUtils;
import com.douqu.game.core.config.FundRewardConfig;
import com.douqu.game.core.config.RechargeGuideConfig;
import com.douqu.game.core.config.RechargeGuideItemConfig;
import com.douqu.game.core.e.E_QueryRechargeCountType;
import com.douqu.game.core.e.E_RechargeGuideItem;
import com.douqu.game.core.e.E_RechargeGuidePage;
import com.douqu.game.core.entity.Player;
import com.douqu.game.core.entity.ext.data.boon.RechargeRecordData;
import com.douqu.game.core.entity.ext.data.boon.SingleBoonData;
import com.douqu.game.core.factory.DataFactory;
import com.douqu.game.core.factory.FunctionFactory;
import com.douqu.game.core.protobuf.SGCommonProto;

import java.util.ArrayList;
import java.util.List;

/**
 * @author wangzhenfei
 *         2018-01-29 17:43
 *         机器人创建辅助类
 */
public class RechargeGuideUtils {

    public static List<SGCommonProto.RechargeGuidePage> getGuideRecharge(Player player) {

        List<SGCommonProto.RechargeGuidePage> pages = new ArrayList<>();
        List<RechargeGuideConfig> guideConfigs = DataFactory.getInstance().getDataList(DataFactory.RECHARGE_GUIDE_KEY);
        List<Integer> temp = null;
        for(RechargeGuideConfig config : guideConfigs){
            if((temp = getPageInfo(player, config)) != null){
                pages.add(SGCommonProto.RechargeGuidePage.newBuilder().setPageId(config.id).addAllCanReciverItemIds(temp).build());
            }else {
                break;
            }
        }

        return pages;
    }

    private static List<Integer> getPageInfo(Player player, RechargeGuideConfig config) {
        if(config.id == E_RechargeGuidePage.NEW_SERVER_FUND.getCode()){
            if(!player.getBonusData().isFundBuy(SGCommonProto.E_FUND_TYPE.FUND_TYPE_NEW_SERVER)){
                return null;
            }
        }else if(config.id == E_RechargeGuidePage.MOUTH_CARD.getCode()){
            if(player.getRechargeData().getRechargeRecordMap().get(config.rechargeType) == null){
                return null;
            }
        }else if(config.id == E_RechargeGuidePage.OPEN_FUND.getCode()){
            if(!player.getBonusData().isFundBuy(SGCommonProto.E_FUND_TYPE.FUND_TYPE_OPEN)){
                return null;
            }
        }else if(config.id == E_RechargeGuidePage.DIAMOND_198.getCode() ||
                config.id == E_RechargeGuidePage.DIAMOND_328.getCode() ||
                config.id == E_RechargeGuidePage.DIAMOND_648.getCode()){
            if(player.getRechargeData().getRechargeRecordMap().get(config.rechargeType) == null){
                return null;
            }
        }
        List<Integer> list = new ArrayList<>();
        for(int itemId : config.itemIds){
           if(itemId == E_RechargeGuideItem.NEW_SERVER_FUND.getCode()){
                if(newServerFund(player)){
                    list.add(itemId);
                }
           }else if(itemId == E_RechargeGuideItem.FIRST_RECHARGE.getCode()){
               if(!player.getBonusData().isFistRechargeReceive()){
                   list.add(itemId);
               }
           }else if(itemId == E_RechargeGuideItem.DAILY_RECHARGE.getCode()){
               if(dailyTotalRecharge(player)){
                   list.add(itemId);
               }
           }else if(itemId == E_RechargeGuideItem.TOTAL_RECHARGE.getCode()){
               if(totalRecharge(player)){
                   list.add(itemId);
               }
           }else if(itemId == E_RechargeGuideItem.VIP_GIFT.getCode()){
               for(int i = 1; i<= player.getVipLevel(); i++){
                   if(!player.getRechargeData().getGiftBagRecordList().contains(i)){
                       list.add(itemId);
                       break;
                   }
               }
           }else if(itemId == E_RechargeGuideItem.DIAMOND.getCode()){
               RechargeGuideItemConfig itemConfig = DataFactory.getInstance().getGameObject(DataFactory.RECHARGE_GUIDE_ITEM_KEY, itemId);
               if(mouthCard(player, itemConfig.functionId)){
                   list.add(itemId);
               }

           }else if(itemId == E_RechargeGuideItem.OPEN_FUND.getCode()){
               if(openFund(player)){
                   list.add(itemId);
               }
           }else if(itemId == E_RechargeGuideItem.ALTAR_EXP.getCode() ||
                   itemId == E_RechargeGuideItem.ALTAR_GOLD.getCode() ||
                   itemId == E_RechargeGuideItem.ALTAR_STRONG_STONE.getCode() ||
                   itemId == E_RechargeGuideItem.ALTAR_EQUIP.getCode() ||
                   itemId == E_RechargeGuideItem.ALTAR_REFINE_STONE.getCode() ||
                   itemId == E_RechargeGuideItem.ALTAR_PROPERTY_WATER.getCode()){
               RechargeGuideItemConfig itemConfig = DataFactory.getInstance().getGameObject(DataFactory.RECHARGE_GUIDE_ITEM_KEY, itemId);
               SingleBoonData singleBoonData = player.getAltarData().altarMap.get(itemConfig.functionId);
               if(singleBoonData != null && singleBoonData.freeCount > 0){
                   list.add(itemId);
               }
           }

        }

        return list;
    }

    public static boolean mouthCard(Player player, int rechargeId){
        RechargeRecordData.MouthCardRecord cardRecord = player.getRechargeData().getRechargeRecordMap().get(rechargeId);
        if (cardRecord != null && DataFactory.currentTime < cardRecord.getEndTime() && //未过期
                !TimeUtils.isToday(cardRecord.getLastRewardTime())) { //今日未领取
            return true;
        }
        return false;
    }

    public static boolean openFund(Player player){
        if(player.getBonusData().isFundBuy(SGCommonProto.E_FUND_TYPE.FUND_TYPE_OPEN)){
            List<FundRewardConfig> rewardConfigs = DataFactory.getInstance().getDataList(DataFactory.FUND_REWARD_KEY);
            for (FundRewardConfig config : rewardConfigs) {
                if(config.type == SGCommonProto.E_FUND_TYPE.FUND_TYPE_OPEN.getNumber()){
                    if (player.getLv() < config.minlv) {
                        return false;
                    } else {
                        if (!player.getBonusData().isFundRewardReceive(SGCommonProto.E_FUND_TYPE.FUND_TYPE_OPEN, config.id)) {
                            return true;
                        }
                    }
                }
            }
        }
        return false;
    }


   public static boolean newServerFund(Player player){
        if(player.getBonusData().isFundBuy(SGCommonProto.E_FUND_TYPE.FUND_TYPE_NEW_SERVER)){
            List<FundRewardConfig> rewardConfigs = DataFactory.getInstance().getDataList(DataFactory.FUND_REWARD_KEY);
            for (FundRewardConfig config : rewardConfigs) {
                if(config.type == SGCommonProto.E_FUND_TYPE.FUND_TYPE_NEW_SERVER.getNumber()){
                    if (player.getBonusData().getLoginTimesByDayForNewFund() < config.minlv) {
                        return false;
                    } else {
                        if (!player.getBonusData().isFundRewardReceive(SGCommonProto.E_FUND_TYPE.FUND_TYPE_NEW_SERVER, config.id)) {
                            return true;
                        }
                    }
                }
            }
        }
        return false;
   }

    public static boolean totalRecharge(Player player){

            List<FundRewardConfig> rewardConfigs = DataFactory.getInstance().getDataList(DataFactory.FUND_REWARD_KEY);
            for (FundRewardConfig config : rewardConfigs) {
                if(config.type == SGCommonProto.E_FUND_TYPE.FUND_TYPE_TOTAL_RECHARGE.getNumber()){
                    if (player.getVipExp() / 10 < config.minlv) {
                        return false;
                    } else {
                        if (!player.getBonusData().isFundRewardReceive(SGCommonProto.E_FUND_TYPE.FUND_TYPE_TOTAL_RECHARGE, config.id)) {
                            return true;
                        }
                    }
                }
            }
        return false;
    }

    public static boolean dailyTotalRecharge(Player player){

        List<FundRewardConfig> rewardConfigs = DataFactory.getInstance().getDataList(DataFactory.FUND_REWARD_KEY);
        int total = player.getRechargeData().getRechargeCount(E_QueryRechargeCountType.DAILY.getCode());
        for (FundRewardConfig config : rewardConfigs) {
            if(config.type == SGCommonProto.E_FUND_TYPE.FUND_TYPE_DAILY_TOTAL_RECHARGE.getNumber()){
                if (total < config.minlv) {
                    return false;
                } else {
                    if (!player.getBonusData().isFundRewardReceive(SGCommonProto.E_FUND_TYPE.FUND_TYPE_DAILY_TOTAL_RECHARGE, config.id)) {
                        return true;
                    }
                }
            }
        }
        return false;
    }


    public static boolean isRechargeGuideShow(Player player) {
        if(!CoreUtils.isFunctionUnLock(FunctionFactory.RECHARGE_SKILL, player))
            return false;

        List<RechargeGuideConfig> guideConfigs = DataFactory.getInstance().getDataList(DataFactory.RECHARGE_GUIDE_KEY);
        for(RechargeGuideConfig config : guideConfigs){
            if(config.id == E_RechargeGuidePage.NEW_SERVER_FUND.getCode()){
                if(!player.getBonusData().isFundBuy(SGCommonProto.E_FUND_TYPE.FUND_TYPE_NEW_SERVER)){
                    return true;
                }
            }else if(config.id == E_RechargeGuidePage.MOUTH_CARD.getCode()){
                if(player.getRechargeData().getRechargeRecordMap().get(config.rechargeType) == null){
                    return true;
                }
            }else if(config.id == E_RechargeGuidePage.OPEN_FUND.getCode()){
                if(!player.getBonusData().isFundBuy(SGCommonProto.E_FUND_TYPE.FUND_TYPE_OPEN)){
                    return true;
                }
            }else if(config.id == E_RechargeGuidePage.DIAMOND_198.getCode() ||
                    config.id == E_RechargeGuidePage.DIAMOND_328.getCode() ||
                    config.id == E_RechargeGuidePage.DIAMOND_648.getCode()){
                if(player.getRechargeData().getRechargeRecordMap().get(config.rechargeType) == null){
                    return true;
                }
            }
        }

        return false;

    }
}
